package com.zbkj.crmeb.store.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zbkj.crmeb.enums.BusinessTypeEnums;
import com.zbkj.crmeb.plat.by.dto.ByCommonConstant;
import com.zbkj.crmeb.store.dao.RelationDao;
import com.zbkj.crmeb.store.dao.TaskDao;
import com.zbkj.crmeb.store.model.Relation;
import com.zbkj.crmeb.store.model.StoreProduct;
import com.zbkj.crmeb.store.model.StoreProductAttrValue;
import com.zbkj.crmeb.store.model.Task;
import com.zbkj.crmeb.store.service.RelationService;
import com.zbkj.crmeb.store.service.StoreProductAttrValueService;
import com.zbkj.crmeb.store.service.StoreProductService;
import com.zbkj.crmeb.store.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Mr.Zhang edited by stivepeim
 * @description StoreOrderServiceImpl 接口实现
 * @date 2020-05-28
 */
@Slf4j
@Service
public class RelationServiceImpl extends ServiceImpl<RelationDao, Relation> implements RelationService, ApplicationRunner {

    @Resource
    private StoreProductService storeProductService;
    @Resource
    private StoreProductAttrValueService storeProductAttrValueService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
//        doHandler();
    }

    private void doHandler(){
        // 捞上架数据
        int count = storeProductService.count(Wrappers.<StoreProduct>lambdaQuery().eq(StoreProduct::getPushPlat, ByCommonConstant.BY_UP));
        int maxPage = (count+499)/500;
        int pageNo = 1;
        while (pageNo<=maxPage){
            Page<StoreProduct> page = PageHelper.startPage(pageNo, 500);
            storeProductService.list(Wrappers.<StoreProduct>lambdaQuery().eq(StoreProduct::getPushPlat, ByCommonConstant.BY_UP).orderByDesc(StoreProduct::getId).orderByDesc(StoreProduct::getId));
            List<StoreProduct> storeProducts = page.getResult();
            ArrayList<Relation> relations = new ArrayList<>();
            ArrayList<Relation> skuRelations = new ArrayList<>();
            for (StoreProduct storeProduct : storeProducts) {
                Relation relation = new Relation();
                relation.setFeature(storeProduct.getFeature());
                relation.setBusinessType(BusinessTypeEnums.PUSH_PRODUCT_PLAT_OF_BY.getCode());
                relation.setBusinessCode(String.valueOf(storeProduct.getId()));
                relation.setSubCode(storeProduct.getPushPlat());
                relation.setGmtCreate(new Date());
                relation.setGmtModify(new Date());
                relations.add(relation);
                List<StoreProductAttrValue> listByProductId = storeProductAttrValueService.getListByProductId(storeProduct.getId());
                if (CollectionUtils.isEmpty(listByProductId)){
                    continue;
                }
                listByProductId.removeIf(v-> !Objects.equals(v.getFeature(ByCommonConstant.PUSH_PLAT),ByCommonConstant.IS_Y));
                if (CollectionUtils.isEmpty(listByProductId)){
                    continue;
                }
                for (StoreProductAttrValue storeProductAttrValue : listByProductId) {
                    Relation skuRelation = new Relation();
                    skuRelation.setFeature(storeProductAttrValue.getFeature());
                    skuRelation.setBusinessType(BusinessTypeEnums.PUSH_SKU_PLAT_OF_BY.getCode());
                    skuRelation.setBusinessCode(String.valueOf(storeProductAttrValue.getId()));
                    skuRelation.setSubCode(ByCommonConstant.BY_UP);
                    skuRelation.setGmtCreate(new Date());
                    skuRelation.setGmtModify(new Date());
                    skuRelations.add(skuRelation);
                }

            }
            if (CollectionUtils.isNotEmpty(relations)){
                List<String> codes = relations.stream().map(k -> k.getBusinessCode()).collect(Collectors.toList());
                List<Relation> list = this.list(Wrappers.<Relation>lambdaQuery().in(Relation::getBusinessCode, codes).eq(Relation::getBusinessType, BusinessTypeEnums.PUSH_PRODUCT_PLAT_OF_BY.getCode()));
                if (CollectionUtils.isNotEmpty(list)){
                    List<String> collect = list.stream().map(k -> k.getBusinessCode()).collect(Collectors.toList());
                    relations.removeIf(k->collect.contains(k.getBusinessCode()));
                }
                if (CollectionUtils.isNotEmpty(relations)){
                    this.saveOrUpdateBatch(relations);
                }
            }
            if (CollectionUtils.isNotEmpty(skuRelations)){
                List<String> codes = skuRelations.stream().map(k -> k.getBusinessCode()).collect(Collectors.toList());
                List<Relation> list = this.list(Wrappers.<Relation>lambdaQuery().in(Relation::getBusinessCode, codes).eq(Relation::getBusinessType, BusinessTypeEnums.PUSH_PRODUCT_PLAT_OF_BY.getCode()));
                if (CollectionUtils.isNotEmpty(list)){
                    List<String> collect = list.stream().map(k -> k.getBusinessCode()).collect(Collectors.toList());
                    skuRelations.removeIf(k->collect.contains(k.getBusinessCode()));
                }
                if (CollectionUtils.isNotEmpty(skuRelations)){
                    this.saveOrUpdateBatch(skuRelations);
                }
            }
            pageNo++;
        }
    }
}

